package com.aaa.forum.services.impl;

import com.aaa.forum.common.AppResult;
import com.aaa.forum.common.ResultCode;
import com.aaa.forum.dao.BookmarkMapper;
import com.aaa.forum.dao.CollectionMapper;
import com.aaa.forum.exception.ApplicationException;
import com.aaa.forum.model.Bookmark;
import com.aaa.forum.model.Collection;
import com.aaa.forum.services.IArticleService;
import com.aaa.forum.services.IBookmarkService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Slf4j
@Service
public class BookmarkServiceImpl implements IBookmarkService {

    @Resource
    private BookmarkMapper bookmarkMapper;

    @Resource
    private CollectionMapper collectionMapper;

    @Resource
    private IArticleService articleService;

    @Override
    public void mark(Long userId, Long articleId) {
        if (userId == null || articleId == null || userId <= 0 || articleId <= 0) {
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        Bookmark bookmark = bookmarkMapper.selectByUserIdAndArticleId(userId, articleId);
        int row;
        if (bookmark == null) {
            // 记录不存在，insert
            bookmark = new Bookmark();
            bookmark.setUserId(userId);
            bookmark.setArticleId(articleId);
            bookmark.setCollectionId(0L);
            bookmark.setState((byte) 1);
            bookmark.setDeleteState((byte) 0);
            Date date = new Date();
            bookmark.setCreateTime(date);
            bookmark.setUpdateTime(date);
            row = bookmarkMapper.insertSelective(bookmark);
            articleService.addOneMarkCountById(articleId);
        } else {
            // 记录已存在，update
            // 构造更新对象，更新数据库

            if (bookmark.getState() == 1 && bookmark.getDeleteState() == 0) {
                // 记录未被删除，且状态为点赞状态
                return;
            }
            Bookmark updateObject = new Bookmark();
            updateObject.setId(bookmark.getId());
            updateObject.setCollectionId(0L);
            updateObject.setState((byte) 1);
            updateObject.setDeleteState((byte) 0);
            updateObject.setUpdateTime(new Date());
            row = bookmarkMapper.updateByPrimaryKeySelective(updateObject);
            articleService.addOneMarkCountById(articleId);
        }
        if (row != 1) {
            log.warn(ResultCode.ERROR_SERVICES.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.ERROR_SERVICES));
        }
    }

    @Override
    public void cancel(Long userId, Long articleId) {
        if (userId == null || userId <= 0 || articleId == null || articleId <= 0) {
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        // 校验是否存在
        Bookmark bookmark = bookmarkMapper.selectByUserIdAndArticleId(userId, articleId);
        if (bookmark == null || bookmark.getDeleteState() == 1 || bookmark.getState() == 0) {
            return;
        }
        Bookmark updateObject = new Bookmark();
        updateObject.setId(bookmark.getId());
        updateObject.setState((byte) 0);
        updateObject.setUpdateTime(new Date());
        int row = bookmarkMapper.updateByPrimaryKeySelective(updateObject);
        if (row != 1) {
            log.warn(ResultCode.ERROR_SERVICES.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.ERROR_SERVICES));
        }
        articleService.subOneMarkCountById(articleId);
    }

    @Override
    public void cancelById(Long id) {
        if (id == null || id <= 0) {
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        // 校验是否存在
        Bookmark bookmark = bookmarkMapper.selectByPrimaryKey(id);
        if (bookmark == null || bookmark.getDeleteState() == 1 || bookmark.getState() == 0) {
            return;
        }
        Bookmark updateObject = new Bookmark();
        updateObject.setId(bookmark.getId());
        updateObject.setState((byte) 0);
        updateObject.setUpdateTime(new Date());
        int row = bookmarkMapper.updateByPrimaryKeySelective(updateObject);
        if (row != 1) {
            log.warn(ResultCode.ERROR_SERVICES.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.ERROR_SERVICES));
        }
        articleService.subOneMarkCountById(bookmark.getArticleId());
    }

    @Override
    public Bookmark selectById(Long id) {
        if (id == null || id <= 0) {
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        return bookmarkMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<Bookmark> selectAllByCollectionId(Long collectionId) {
        // 非空校验
        if (collectionId == null || collectionId < 0) {
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        // 校验收藏夹是否存在
        Collection collection = collectionMapper.selectByPrimaryKey(collectionId);
        if (collection == null || collection.getDeleteState() == 1) {
            log.warn(ResultCode.FAILED_COLLECTION_NOT_EXISTS.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_COLLECTION_NOT_EXISTS));
        }
        // 返回数据
        return null;
    }

    @Override
    public void modifyCollectionId(Long id, Long collectionId) {
        if (id == null || id <= 0) {
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        // 校验是否存在
        Bookmark bookmark = bookmarkMapper.selectByPrimaryKey(id);
        if (bookmark == null || bookmark.getDeleteState() == 1 || bookmark.getState() == 0) {
            return;
        }
        // 构造更新对象
        Bookmark updateObject = new Bookmark();
        updateObject.setId(bookmark.getId());
        updateObject.setCollectionId(collectionId);
        updateObject.setUpdateTime(new Date());
        int row = bookmarkMapper.updateByPrimaryKeySelective(updateObject);
        if (row != 1) {
            log.warn(ResultCode.ERROR_SERVICES.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.ERROR_SERVICES));
        }
    }

    @Override
    public void deleteByCollection(Long collectionId) {

    }

    @Override
    public Byte getState(Long userId, Long articleId) {
        if (userId == null || userId <= 0 || articleId == null || articleId <= 0) {
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        return bookmarkMapper.getState(userId, articleId);
    }

    @Override
    public int moveToDefault(Long collectionId) {
        if (collectionId == null || collectionId <= 0) {
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        return bookmarkMapper.moveToDefault(collectionId);
    }

    @Override
    public int getCountByCollectionId(Long collectionId) {
        if (collectionId == null || collectionId <= 0) {
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        return bookmarkMapper.getCountByCollectionId(collectionId);
    }
}
